// import HttpUtils from 'pages/HttpUtils/HttpUtils';
// 获取当前时间戳
var timestamp = Date.parse(new Date()) / 1000;
const app = getApp();
Page({
	// ...HttpUtils.func,
	data: {
		titOn: 'all',
		voteId: '',
		totalPages: 0,
		partyVoteDetail: [],
		select: false,

		optionIds: '',		//存放选中的值
		title: '',
		sponsor: '',
		status: '',
		startDate: '',
		partyName: '',
		address: '',
		subject: '',
		sign: '',
		isSubmit: false,
	},
	onLoad(params) {
		var voteId = params.voteId;
		this.setData({
			voteId: voteId
		});
		//投票基本信息查询接口
		this.loadpartyVoteDetail();
	},

	//投票基本信息查询接口
	loadpartyVoteDetail: function(e) {
		var newUrl = getApp().globalData.dangjianUrl + "/vote/partyVoteDetail";
		var params = {};
		params["voteId"] = this.data.voteId + "";
		params = JSON.stringify(params);
		app.httpGet(newUrl, params, "投票基本信息查询接口调用失败").then(
			(respData) => {
				console.log(respData)
				var result = respData.data.result.code;
				if (1 == result) {
					var votes = respData.data.vote;
					var DateTime;
					if (votes.startDate != null && votes.endDate != null) {
						DateTime = getApp().formatToDate(votes.startDate, votes.endDate);
					}
					this.setData({
						"voteType": votes.voteType,
						"partyVoteDetail": votes.options,
						"title": votes.title,
						"sponsor": votes.userName,
						"status": votes.status,
						"DateTime": DateTime,
						"partyName": votes.partyName,
						"subject": votes.subject,
					});
        } else {
			app.showPageMsg("alert", respData.data.result.description) 
				}
			}
		);
	},


	//人员投票提交接口 
	loadsubmit: function(e) {
		if (this.data.optionIds != '' && !this.data.isSubmit) {
			var newUrl = getApp().globalData.dangjianUrl + "/vote/partyUserVote"; 

			var params = {};
			params["voteId"] = this.data.voteId + "";
			params["optionIds"] = this.data.optionIds + "";
			params = JSON.stringify(params);
			this.httpGet(newUrl, params, "人员投票提交接口 调用失败").then(
				(respData) => {
					console.log(respData)
					var result = respData.data.result.code;
					if (1 == result) {
						var pages = getCurrentPages();
						var prevPage = pages[pages.length - 2];
						var partyVoteList = prevPage.data.partyVoteList;
						for (var i in partyVoteList) {
							if (partyVoteList[i].voteId == this.data.voteId) {
								partyVoteList[i].voteStatus = 1
							}
						}
						prevPage.loadpartyVoteList();

						this.setData({
							"isSubmit": true,		//是否提交过
						})
						var voteId = this.data.voteId;
						var pageUrl = "../voteEnd/voteEnd?voteId=" + voteId;
						wx.navigateTo({
							url: pageUrl
						})
          } else {
            this.showPageMsg("alert", respData.data.result.description) 
					}
				}
			);
		} else {
			if (this.data.isSubmit) {
				this.showPageMsg("tips", "您已经投过票了，不能多次投票!");
			} else if (this.data.optionIds == '') {
				this.showPageMsg("tips", "投票后才能提交!");
			}
		}
	},

	//点击选择
	selectChoice: function(e) {
		var index = e.target.dataset.index;
		this.setData({
			select: !this.data.select,
			sign: index
		})
	},
	//复选框选择
	onChangeCheckbox(e) {
		var voteType = e.currentTarget.dataset.voteType;
		var length = e.detail.value.length;
		var detail = this.data.partyVoteDetail;
		if (length == parseInt(voteType)) {
			var value = e.detail.value;
			for (var index in detail) {
				var id = detail[index].optionId + "";
				if (JSON.stringify(value).indexOf(id) == -1) {
					detail[index]['disabled'] = true;
				}
			}
		} else {
			for (var index in detail) {
				detail[index]['disabled'] = false;
			}
		}
		this.setData({
			partyVoteDetail: detail,
			optionIds: `${e.detail.value}`,
		});
		// if (length <= parseInt(voteType)) {
		// 	this.setData({

		// 	})
		// } else {

		// }


	},
	//单选框选择
	onChangeRadio(e) {
		var length = e.detail.value.length;
		this.setData({
			optionIds: `${e.detail.value}`
		})
	},
	onReachBottom() {
		// 页面被拉到底部
		// if (this.data.pageIndex > this.data.totalPages) {
		//   this.showPageMsg("tips","没有更多数据了!");
		// } else {
		//   this.loadMsg();
		// }
	},
	onPullDownRefresh: function() {
		// this.setData({
		//   pageIndex:1,
		//   msgList:[]
		// });
		// this.loadMsg();
		// wx.stopPullDownRefresh();
	},
});
